package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.KhjcAdmonishConvert;
import com.engine4cloud.tdf.supervision.dto.create.KhjcAdmonishCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.KhjcAdmonishQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.KhjcAdmonishResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.KhjcAdmonishUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.KhjcAdmonishEntity;
import com.engine4cloud.tdf.supervision.mapper.KhjcAdmonishMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.KhjcAdmonishService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 训诫
 *
 * @author wenjie
 * @date 2024-05-31 16:01:12
 */
@Service
public class KhjcAdmonishServiceImpl extends ServiceImpl<KhjcAdmonishMapper, KhjcAdmonishEntity> implements KhjcAdmonishService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<KhjcAdmonishResultDTO> pageList(Params pageable, KhjcAdmonishQueryDTO dto) {
        Page<KhjcAdmonishEntity> page = page(Pages.page(pageable), wrapper(dto));
        return Pages.convert(page, KhjcAdmonishConvert.INSTANCE::convert);
    }

    @Override
    public KhjcAdmonishResultDTO getById(Long id) {
        KhjcAdmonishResultDTO result = Optional.ofNullable(KhjcAdmonishConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        //设置法律文书信息
        this.setInstrumentList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(KhjcAdmonishCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));
        TdfUser user = SecurityUtils.getUser();

        KhjcAdmonishEntity entity = KhjcAdmonishConvert.INSTANCE.create(dto, dx.getTenantId());
        super.save(entity);
        //保存文件信息
        this.saveInstrumentList(dto, entity, user);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(KhjcAdmonishUpdateDTO dto) {
        KhjcAdmonishEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        TdfUser user = SecurityUtils.getUser();

        super.updateById(KhjcAdmonishConvert.INSTANCE.update(dto));
        //保存文件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    private void saveInstrumentList(KhjcAdmonishCreateDTO dto, KhjcAdmonishEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_ADMONISH_PUNISHMENT), dto.getInstrumentList(), user);
    }

    public void updateInstrumentList(KhjcAdmonishUpdateDTO dto, KhjcAdmonishEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_ADMONISH_PUNISHMENT), dto.getInstrumentList(), user);
    }

    private void setInstrumentList(KhjcAdmonishResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_ADMONISH_PUNISHMENT);
        dto.setInstrumentList(fileService.findList(query));
    }


    private LambdaQueryWrapper<KhjcAdmonishEntity> wrapper(KhjcAdmonishQueryDTO dto) {
        LambdaQueryWrapper<KhjcAdmonishEntity> wrapper = Queries.lambda();
        Queries.accept(dto.getId(), v -> wrapper.eq(KhjcAdmonishEntity::getId, v));
        Queries.accept(dto.getRybh(), v -> wrapper.eq(KhjcAdmonishEntity::getRybh, v));
        Queries.accept(dto.getLcbh(), v -> wrapper.eq(KhjcAdmonishEntity::getLcbh, v));
        return wrapper;
    }
}